03. Variables
Variables Heading
Variables
ND079 C1 L1 A03a Variables And State Data
What is a Variable?
Variables are used to store data for our application. A variable is a combination of:
- A location in the computer's memory and
- An associated name that we can use in our code to refer to the data in that memory location
Specifically, variables help us store state data.
State data is data related to the current state of the program as it is running.
Using Variables in Java
To declare or define a variable in Java, we specify the data type, the name of the variable, and any value we want to store in the variable. For example, here we declare an integer value with the name age
that holds the value 42
:
int age = 42;
Now when we type the word age
in our code, it will refer to this location in memory.
The reason we use the term "variable" is because it is able to vary. After we have declared it, we can easily change the value of the variable as many times as we like:
int age = 42;
age = 43;
age = 44;
These lines are referred to as assignment statements(because we are assigning the value on the right to the variable named on the left) and the =
sign is called the assignment operator.
Static vs Dynamic Typing
ND079 C1 L1 A03b Static Vs Dynamic Typing
Notice that, in Java, we must specify the data type (e.g., int
). This is becouse Java is a statically typed language. Here are the key points to remember about static typing:
- The data type is bound to the variable when the variable is first declared.
- The data type is checked when the code is compiled.
- The data type for a variable cannot later be changed.
In contrast, some languages are dynamically typed. In dynamic typing:
- The data type is bound to the value itself, but not to the variable.
- The type is checked during runtime.
- The data type of a variable can be changed after it is declared; since the type is associated with the value, assigning a new value may mean changing the data type.
Python is a popular example of a dynamically typed language. For example, in Python, you can declare a variable without explicitly stating the type:
age = 42
And you can later assign a value of a different type to that variable:
age = "forty-two"
And this will work—it will not throw a type error (as it would in Java), because the type is associated with the value, rather than being associated with the variable.
However, this could cause other problems or unintended behavior in the application. Perhaps the application later makes a calculation based on the user's age, and now that the age is stored as a string, that calculation will fail. Thus, dynamic typing, while it has its advantages, can lead to errors or unexpected behavior.
In contrast, one of the nice things about Java being statically typed is that you declare the data type of the variable before the code even runs, and you can be sure that no matter what new values the variable takes on, it will always be the same type of data.
The Semicolon ;
Ends a Line
ND079 C1 L1 A03c Semicolons
Notice that Java ends every statement with a semicolon, ;
. This is how Java knows it has reached the end of the statement. If we leave the semicolon off the ends of these lines:
int age = 42
age = 43
Then Java will read them as a single line:
int age = 42age = 43
And this would cause an error.